dim xrot#           ' X rotation
dim yrot#           ' Y rotation

dim texture         ' Storage for one texture
dim box             ' Storage for the display list
dim top             ' Storage for the second display list
dim xloop           ' Loop for X axis
dim yloop           ' Loop for Y axis

dim boxcol#(4)(2)   ' Array for box colors
boxcol#(0) = vec3 ( 1, 0, 0)                ' Bright:  Red, Orange, Yellow, Green, Blue
boxcol#(1) = vec3 ( 1,.5, 0)
boxcol#(2) = vec3 ( 1, 1, 0)
boxcol#(3) = vec3 ( 0, 1, 0)
boxcol#(4) = vec3 ( 0, 1, 1)

dim topcol#(4)(2)   ' Array for top colors
topcol#(0) = vec3 ( .5,  0,  0)             ' Dark:  Red, Orange, Yellow, Green, Blue
topcol#(1) = vec3 ( .5,.25,  0)
topcol#(2) = vec3 ( .5, .5,  0)
topcol#(3) = vec3 (  0, .5,  0)
topcol#(4) = vec3 (  0, .5, .5)

' Load a single texture
texture = LoadMipmapTexture ("Data\Cube.bmp")

' Build cube display lists
box = glGenLists(2)                 ' Generate 2 different lists
glNewList(box, GL_COMPILE)          ' Start with the box list
    glBegin(GL_QUADS)

        ' Bottom Face
        glNormal3f( 0.0,-1.0, 0.0)
        glTexCoord2f(1.0, 1.0): glVertex3f(-1.0, -1.0, -1.0)
        glTexCoord2f(0.0, 1.0): glVertex3f( 1.0, -1.0, -1.0)
        glTexCoord2f(0.0, 0.0): glVertex3f( 1.0, -1.0,  1.0)
        glTexCoord2f(1.0, 0.0): glVertex3f(-1.0, -1.0,  1.0)
            
        ' Front Face
        glNormal3f( 0.0, 0.0, 1.0)
        glTexCoord2f(0.0, 0.0): glVertex3f(-1.0, -1.0,  1.0)
        glTexCoord2f(1.0, 0.0): glVertex3f( 1.0, -1.0,  1.0)
        glTexCoord2f(1.0, 1.0): glVertex3f( 1.0,  1.0,  1.0)
        glTexCoord2f(0.0, 1.0): glVertex3f(-1.0,  1.0,  1.0)

        ' Back Face
        glNormal3f( 0.0, 0.0,-1.0)
        glTexCoord2f(1.0, 0.0): glVertex3f(-1.0, -1.0, -1.0)
        glTexCoord2f(1.0, 1.0): glVertex3f(-1.0,  1.0, -1.0)
        glTexCoord2f(0.0, 1.0): glVertex3f( 1.0,  1.0, -1.0)
        glTexCoord2f(0.0, 0.0): glVertex3f( 1.0, -1.0, -1.0)
        
        ' Right face
        glNormal3f( 1.0, 0.0, 0.0)
        glTexCoord2f(1.0, 0.0): glVertex3f( 1.0, -1.0, -1.0)
        glTexCoord2f(1.0, 1.0): glVertex3f( 1.0,  1.0, -1.0)
        glTexCoord2f(0.0, 1.0): glVertex3f( 1.0,  1.0,  1.0)
        glTexCoord2f(0.0, 0.0): glVertex3f( 1.0, -1.0,  1.0)
        
        ' Left Face
        glNormal3f(-1.0, 0.0, 0.0)
        glTexCoord2f(0.0, 0.0): glVertex3f(-1.0, -1.0, -1.0)
        glTexCoord2f(1.0, 0.0): glVertex3f(-1.0, -1.0,  1.0)
        glTexCoord2f(1.0, 1.0): glVertex3f(-1.0,  1.0,  1.0)
        glTexCoord2f(0.0, 1.0): glVertex3f(-1.0,  1.0, -1.0)
    glEnd()
glEndList()
top = box + 1                       ' Storage for "Top" is "Box" plus one
glNewList(top,GL_COMPILE)           ' Now the "Top" display list
    glBegin(GL_QUADS)
        ' Top Face
        glNormal3f( 0.0, 1.0, 0.0)
        glTexCoord2f(0.0, 1.0): glVertex3f(-1.0,  1.0, -1.0)
        glTexCoord2f(0.0, 0.0): glVertex3f(-1.0,  1.0,  1.0)
        glTexCoord2f(1.0, 0.0): glVertex3f( 1.0,  1.0,  1.0)
        glTexCoord2f(1.0, 1.0): glVertex3f( 1.0,  1.0, -1.0)
    glEnd()
glEndList()

' Setup OpenGL
glEnable(GL_LIGHT0)                 ' Quick and dirty lighting (assumes Light0 is set up)
glEnable(GL_LIGHTING)               ' Enable lighting
glEnable(GL_COLOR_MATERIAL)         ' Enable material coloring
glEnable(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, texture)

' Main loop
while true
    
    ' Render
    glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)     ' Clear screen and depth buffer
    glBindTexture(GL_TEXTURE_2D, texture)
    for yloop = 1 to 5
        for xloop = 0 to yloop - 1
            glLoadIdentity ()                               ' Reset the view
            glTranslatef(1.4+xloop*2.8-yloop*1.4,(6.0-yloop)*2.4-7.0,-20.0)
            glRotatef(45.0-2.0*yloop+xrot#,1.0,0.0,0.0)
            glRotatef(45.0+yrot#,0.0,1.0,0.0)
            glColor3fv(boxcol#(yloop-1))
            glCallList(box)
            glColor3fv(topcol#(yloop-1))
            glCallList(top)
        next
    next
    SwapBuffers ()

    ' Handle input
    if ScanKeyDown (VK_LEFT) then
        yrot# = yrot# - 0.2
    endif
    if ScanKeyDown (VK_RIGHT) then
        yrot# = yrot# + 0.2
    endif
    if ScanKeyDown (VK_UP) then
        xrot# = xrot# - 0.2
    endif
    if ScanKeyDown (VK_DOWN) then
        xrot# = xrot# + 0.2
    endif

wend    